package com.real.fact.web.sale.application;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.real.fact.web.application.FillOutputStreamCommand;
import com.real.fact.web.sale.domain.Sale;
import com.real.fact.web.sale.domain.SaleFilter;
import com.real.fact.web.sale.domain.SaleRepository;
import com.real.fact.web.sale.domain.Ticket;

public class ExportXLSSalesCommand extends FillOutputStreamCommand {

  private final SaleRepository saleRepository;

  private OutputStream outputStream;
  
  private Date dateFrom;

  private Date dateEnd;

  private boolean facturaA;

  private boolean facturaB;

  private boolean remito;

  private boolean noFact;

  public ExportXLSSalesCommand(final SaleRepository theSaleRepository) {
    saleRepository = theSaleRepository;
  }

  public Void execute() {
    List<Sale> sales = retrieveData();
    Workbook wb = new HSSFWorkbook(); //for xls
    // Workbook wb = new XSSFWorkbook(); for xlsx
    CreationHelper createHelper = wb.getCreationHelper();
    Sheet sheet = wb.createSheet("Page1");
    //Set some formats stuff
    final short dateFormat = createHelper.createDataFormat().getFormat("dd/MM/yyyy HH:mm:ss");
    final CellStyle dateCellStyle = wb.createCellStyle();
    dateCellStyle.setDataFormat(dateFormat);
    
    //Set headers
    Row row = sheet.createRow(0);
    row.createCell(0).setCellValue("ID");
    row.createCell(1).setCellValue("Date");
    row.createCell(2).setCellValue("Payment");
    row.createCell(3).setCellValue("Ticket Type");
    row.createCell(4).setCellValue("Total");
    
    int i = 1;
    for (Sale sale : sales) {
      // Create a row and put some cells in it. Rows are 0 based.
      row = sheet.createRow(i);
      // Create a cell and put a value in it.
      row.createCell(0).setCellValue(sale.getId());
      row.createCell(1).setCellValue(sale.getDate());
      row.getCell(1).setCellStyle(dateCellStyle);
//      row.createCell(2).setCellValue(sale.getPayment().toString());
//      row.createCell(3).setCellValue(sale.getTicket().toString());
      row.createCell(4).setCellValue(sale.getTotal());
      i++;
    }
    sheet.autoSizeColumn(0);
    sheet.autoSizeColumn(1);
    sheet.autoSizeColumn(2);
    sheet.autoSizeColumn(3);
    sheet.autoSizeColumn(4);
    try {
      wb.write(outputStream);
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    return null;
  }

  private List<Sale> retrieveData() {
    SaleFilter filter = new SaleFilter(dateFrom, dateEnd);
    if (facturaA) {
      filter.addTicketType(Ticket.A);
    }
    if (facturaB) {
      filter.addTicketType(Ticket.B);
    }
    if (remito) {
      filter.addTicketType(Ticket.R);
    }
    if (noFact) {
      filter.addTicketType(Ticket.NONE);
    }
    return saleRepository.filter(null, filter);
  }

  public void setOutputStream(OutputStream outputStream) {
    this.outputStream = outputStream;
  }

  public void setDateFrom(Date dateFrom) {
    this.dateFrom = dateFrom;
  }

  public void setDateEnd(Date dateEnd) {
    this.dateEnd = dateEnd;
  }

  public void setFacturaA(boolean facturaA) {
    this.facturaA = facturaA;
  }

  public void setFacturaB(boolean facturaB) {
    this.facturaB = facturaB;
  }

  public void setRemito(boolean remito) {
    this.remito = remito;
  }

  public void setNoFact(boolean noFact) {
    this.noFact = noFact;
  }

  @Override
  public String getFileName() {
    return "ventas_";
  }

}
